package com.cg.asm.body.controller.roles;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.body.domain.dto.roles.RolesDTO;
import com.cg.asm.body.domain.vo.roles.RolesChoiceVO;
import com.cg.asm.body.domain.vo.roles.RolesPagedVO;
import com.cg.asm.body.service.roles.IRolesInfoService;
import com.cg.asm.body.util.constants.DicEnum;
import com.cg.asm.body.util.constants.EnumGroup;
import com.cg.asm.client.CommonUtil;
import com.cg.asm.client.exception.MybatisExample;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "RolesController", value = "角色接口")
@RestController
@RequestMapping("/roles")
public class RolesController {

    private final IRolesInfoService rolesInfoService;

    public RolesController(IRolesInfoService rolesInfoService) {
        this.rolesInfoService = rolesInfoService;
    }

    @ApiOperation(value = "角色列表查询方法（分页）", notes = "角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页尺寸", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sortBy", value = "排序项", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "descending", value = "正序/倒序", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "filter", value = "过滤条件", dataType = "String", paramType = "query")
    })
    @GetMapping("/pagedlist")
    public JSONObject pagedlist(String pageIndex, String pageSize, String sortBy, boolean descending, String filter) {
        PageInfo<RolesPagedVO> contentsPaginator = rolesInfoService.selectByRolesInfo(MybatisExample.initPagedInfo(
                pageIndex,
                pageSize,
                sortBy,
                descending,
                filter
        ));
        return CommonUtil.successJson(contentsPaginator);
    }

    @ApiOperation(value = "获取系统内置角色类别", notes = "角色")
    @GetMapping("/roleTypeOptions")
    public JSONObject roleTypeOptions() {
        return CommonUtil.successJson(DicEnum.getEnum(EnumGroup.ROLE_TYPE));
    }

    @ApiOperation(value = "校验角色是否唯一", notes = "角色")
    @GetMapping("/roleIsUnique/{roleId}")
    public JSONObject roleIsUnique(@PathVariable String roleId) {
        boolean unique = rolesInfoService.roleIsUnique(roleId);
        return CommonUtil.successJson(unique);
    }

    @ApiOperation(value = "保存角色信息", notes = "角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolesDTO", value = "角色实体对象", required = true, dataType = "RolesDTO", paramType = "body")
    })
    @PostMapping("/saveNewRole")
    public JSONObject saveNewRole(RolesDTO rolesDTO) {
        try {
            rolesInfoService.saveNewRole(rolesDTO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "保存编辑的角色信息", notes = "角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolesDTO", value = "角色实体对象", required = true, dataType = "RolesDTO", paramType = "body")
    })
    @PutMapping("/saveEditRole")
    public JSONObject saveEditRole(RolesDTO rolesDTO) {
        try {
            rolesInfoService.saveEditRole(rolesDTO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据角色ID删除角色信息", notes = "角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleIds", value = "角色ID数组", required = true, dataType = "String[]", paramType = "body")
    })
    @DeleteMapping("/delRole")
    public JSONObject delRole(@RequestBody String[] roleIds) {
        try {
            rolesInfoService.delRole(roleIds);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "获取可选择的所有执行类角色", notes = "角色")
    @GetMapping("/roleByExecute")
    public JSONObject roleByExecute() {
        List<RolesChoiceVO> rolesChoiceVO = rolesInfoService.roleByExecute();
        return CommonUtil.successJson(rolesChoiceVO);
    }

    @ApiOperation(value = "获取可选择的所有管理类角色", notes = "角色")
    @GetMapping("/roleByManager")
    public JSONObject roleByManager() {
        List<RolesChoiceVO> rolesChoiceVO = rolesInfoService.roleByManager();
        return CommonUtil.successJson(rolesChoiceVO);
    }

}
